package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.UserCouponListAdapter;
import com.afd.ozblip.entity.CouponEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class UserCouponActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private ListView couponListView;
	private TextView tipTv;
	/****************** 其它变量 **************************/
	private List<CouponEntity> couponList;
	private UserCouponListAdapter couponListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_coupon_list);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();

	}

	@Override
	protected void onResume() {
		super.onResume();
		// 获取列表数据
		requestTopListData();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.couponListView = (ListView) findViewById(R.id.lv_coupon_city);
		this.tipTv = (TextView) findViewById(R.id.tip_text_noData);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {

		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(UserCouponActivity.this,
							SettingActivity.class);
					startActivity(settingIntent);
					break;

				default:
					break;
				}
			}
		};
		this.couponListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> itemView, View arg1,
					int parameInt, long arg3) {
				Intent businessIntent = new Intent(UserCouponActivity.this,
						UserCouponDetailActivity.class);
				CouponEntity coupon = UserCouponActivity.this.couponList
						.get(parameInt);
				businessIntent.putExtra("coupon", coupon);
				startActivity(businessIntent);
			}
		});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	/**
	 * 获取评论列表数据
	 */
	private void requestTopListData() {
		if (commonUserInfo == null) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyVipParamMap = new HashMap<String, Object>();
						nearbyVipParamMap.put("userId",
								commonUserInfo.getUserId());
						nearbyVipParamMap.put("pageCnt", "1");
						nearbyVipParamMap.put("pageRow", "20");
						nearbyVipParamMap.put("sort", "1");
						nearbyVipParamMap.put("language", commonLanguage);
						// 发送请求
						byte[] bCouponList = HttpUtil.requestByPost(
								UserCouponActivity.this,
								Constants.USER_COUPON_LIST_API,
								nearbyVipParamMap);
						if (bCouponList != null) {
							couponList = new ParseJson()
									.parseUserCouponListJson(bCouponList);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						if (couponList != null && couponList.size() > 0) {
							setUserCouponListAdapter();
						} else {
							tipTv.setVisibility(0);
							tipTv.setText(R.string.noData_text);
						}
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用优惠券列表适配器
	 */
	public void setUserCouponListAdapter() {
		if (CollectionUtil.UnNullAndZero(couponList)) {
			this.couponListAdapter = new UserCouponListAdapter(
					UserCouponActivity.this);
			this.couponListAdapter.addItems(couponList);
			this.couponListView.setAdapter(this.couponListAdapter);

		}
	}
}